# This code is hosted on http://code.google.com/p/lenthorp/
# Freely available for use in applications, but should NOT be modified
# Email all comments to lenthorpresearch@gmail.com

from types import IntType, LongType, FloatType
from xml.dom.minidom import parse, parseString

def LoadFromXML(filePath):
    # parses a simple XML file (see example) and returns a map preserving the tree structure
    ###ret = {}
    tree = parse(filePath)
    node = tree.getElementsByTagName('Params')[0]
    ret = LoadToMap(node)
    return ret

def LoadToMap(node):
    # designed to be called recursively
    ret = {}
    for subNode in node.childNodes:
        isMap = True
        if subNode.nodeType == subNode.ELEMENT_NODE:
            for subSubNode in subNode.childNodes:
                if subSubNode.nodeType == subSubNode.TEXT_NODE and not subSubNode.hasChildNodes():
                    if not '\t' in subSubNode.nodeValue and not '\n' in subSubNode.nodeValue:
                        isMap = False
            if isMap:
                ret[subNode.nodeName.encode()] = LoadToMap(subNode)
            else:
                if subNode.attributes.has_key('type'):
                    if subNode.getAttribute('type') == 'float':
                        ret[subNode.nodeName.encode()] = float(subNode.childNodes[0].nodeValue.encode())
                    elif subNode.getAttribute('type') == 'int':
                        ret[subNode.nodeName.encode()] = int(subNode.childNodes[0].nodeValue.encode())
                    else:
                        ret[subNode.nodeName.encode()] = subNode.childNodes[0].nodeValue.encode()
                else:
                    ret[subNode.nodeName.encode()] = subNode.childNodes[0].nodeValue.encode()
    return ret